import { CreateUserDto } from './dto/create-user.dto';
import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Request,
  UseGuards,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { ResponseData } from '../../common/interfaces/result.interface';
import { UserService } from './user.service';
import { resSuccess } from '../../common/utils/response.util';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { userInfo } from 'os';

@ApiTags('用户')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get()
  @ApiQuery({ name: 'pageNum', required: false })
  @ApiQuery({ name: 'username', required: false })
  @ApiQuery({ name: 'nickname', required: false })
  async index(@Query() query): Promise<ResponseData> {
    return this.userService.findList(query);
  }

  @ApiResponse({
    status: 201,
    description: 'The article has been successfully created.',
  })
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  @Post()
  async create(@Body() body: CreateUserDto) {
    return this.userService.create(body);
  }

  @Put('setUserData')
  async setUserData(@Body() body) {
    console.log('body', body);
    return this.userService.setUserData(body);
  }

  @Get('getuser')
  async getUser() {
    return this.userService.getUser(1);
  }

  @Get('userrole/:userId')
  async getUserRole(@Param('userId') userId: number) {
    return this.userService.getUserRole(userId);
  }

  @Get('userInfo')
  async my(@Request() req) {
    return this.userService.getUser(req.user.userId);
  }

  // @Get('mypmss')
  // async getUserPermissions() {
  //   return this.userService.getUserPermissions()
  // }

  @Post('setrole/:userId/')
  @ApiOperation({ summary: '根据用户id删除用户与角色关系' })
  async setUserRole(
    @Param('userId') userId: number,
    @Body('role') role: string[],
  ): Promise<ResponseData> {
    console.log('userId', userId);
    console.log('role', role);
    return this.userService.setUserRole(userId, role);
  }

  @Put('resetPassword/:userId')
  async resetPassword(@Param('userId') userId: number, @Body('password') body) {
    return this.userService.resetPassword(userId, body);
  }
  @Delete(':userId')
  @ApiOperation({ summary: '根据用户id删除用户与角色关系' })
  async deleteuser(@Param('userId') userId: number): Promise<ResponseData> {
    return this.userService.delete(userId);
  }
}
